package Repositorio;

import View.Novo;
import View.Ver;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Random;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

public class RepositorioOs {

    private Connection conn;
    private String query;
    private PreparedStatement stmt;
    private ResultSet rs;
    int random;

    

    public void salvar(long idOs, int idLogin_ref, int idEquipamento, int idCliente, String defeito_ref,
            String solucao_ref, boolean urgencia, boolean statusCliente_ref) throws SQLException {

        Date data_ref = new java.sql.Date(new java.util.Date().getTime());

        conn = LinkDB.Conexao.getConexao();
        query = "INSERT INTO os(idOs, idLogin, idEquipamento, idCliente, defeito, "
                + "solucao, urgencia, dataOs, statusOs) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)";
        stmt = conn.prepareStatement(query);
        stmt.setLong(1, idOs);
        stmt.setInt(2, idLogin_ref);
        stmt.setInt(3, idEquipamento);
        stmt.setInt(4, idCliente);
        stmt.setString(5, defeito_ref);
        stmt.setString(6, solucao_ref);
        stmt.setBoolean(7, urgencia);
        stmt.setDate(8, data_ref);
        stmt.setBoolean(9, statusCliente_ref);
        stmt.executeUpdate();
        JOptionPane.showMessageDialog(null, "Chamado registrado com sucesso.");
        stmt.close();
        conn.close();
    }

    public boolean verifyRandom() throws SQLException { 
        random = (new Random().nextInt(999999));
        conn = LinkDB.Conexao.getConexao();
        query = "SELECT idOs FROM os WHERE idOs = ?";
        stmt = conn.prepareStatement(query);
        stmt.setInt(1, random);
        rs = stmt.executeQuery();
        if (rs.next()) {
            return true;
        } else {
            return false;
        }
    }

    public void setProtocoloOs() throws SQLException {
        while (verifyRandom() == true) {
            verifyRandom();
        }
        Novo.protocolo.setText(String.valueOf(random));
    }
    
    public void verChamadosAbertos() throws SQLException{
        conn = LinkDB.Conexao.getConexao();
        query = "SELECT cliente.nameCliente, cliente.contactCliente, cliente.setor, "
                + "cliente.endereco, cliente.tellCliente, os.idOs, os.defeito, os.solucao, "
                + "os.urgencia, os.dataOs, equipamento.nameEquipamento, login.nameLogin "
                + "FROM cliente, os, equipamento, login WHERE (cliente.idCliente = os.idCliente) "
                + "AND (equipamento.idEquipamento = os.idEquipamento) AND (login.idLogin = os.idLogin) "
                + "AND os.statusOs = '0' ORDER BY os.urgencia DESC, os.dataOs DESC";
        stmt = conn.prepareStatement(query);        
        rs = stmt.executeQuery();
        DefaultTableModel model = (DefaultTableModel) Ver.jTable1.getModel();        
        while(rs.next()){
            int a = model.getRowCount() + 1;
            model.addRow(new Object[]{
                a,
                rs.getString("cliente.nameCliente"),
                rs.getString("cliente.endereco"),
                rs.getString("cliente.contactCliente"),
                rs.getString("cliente.setor"),
                rs.getString("cliente.tellCliente"),
                rs.getString("equipamento.nameEquipamento"),
                rs.getString("os.defeito"),
                rs.getString("os.idOs")
            });
        }
        rs.close();
        stmt.close();
        conn.close();
    }
}
